



// 全局字典项
const state = {
    Dist: {},
};
const getters = {};
const mutations = {

};

// 公用字典项请求缓存队列
var CommonDistQueue = {};
var CommonDistQueueEvent = async function (type) {
    // 实际接口应用时进行安装
    var res = await Common.getDist({ dmlx: type });
    return res;
};
const actions = {
    // 获取通用字典项
    async getCommonDist({ state }, type) {
        if (type && state.Dist[type]) {
            return JSON.parse(JSON.stringify(state.Dist[type]))
        } else if (type && !state.Dist[type]) {
            if (CommonDistQueue[type]) {
                return CommonDistQueue[type];
            } else {
                var _event = CommonDistQueueEvent(type);
                CommonDistQueue[type] = _event;
                try {
                    var res = await _event;
                    state.Dist[type] = res;
                    delete CommonDistQueue[type];
                    return res;
                } catch (error) {
                    delete CommonDistQueue[type];
                    throw error;
                };
            }
        }
    },
    // 格式化通用字典项
    async formatCommonDistByKey({ dispatch }, options) {
        var type = options.type;
        var key = options.key || 'DM';
        try {
            var res = await dispatch('getCommonDist', type);
            var _data = {};
            res.forEach(item => {
                _data[item[key]] = item;
            });
            return _data
        } catch (error) {
            console.error('获取字典项失败,请检查配置项 ：' + options.type, error);
        }
    },
};
export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}